import os
import json

def rename_resources():
    current_dir = os.getcwd()
    xhdpi_dir = os.path.join(current_dir, 'xhdpi')
    xxhdpi_dir = os.path.join(current_dir, 'xxhdpi')
    record_file = os.path.join(current_dir, 'processed_files.json')
    
    if not os.path.exists(xhdpi_dir):
        print('错误: xhdpi 目录不存在')
        return
        
    if not os.path.exists(xxhdpi_dir):
        print('错误: xxhdpi 目录不存在')
        return
    
    # 读取已处理文件记录
    processed_files = {'xhdpi': [], 'xxhdpi': []}
    if os.path.exists(record_file):
        with open(record_file, 'r', encoding='utf-8') as f:
            processed_files = json.load(f)
    
    for filename in os.listdir(xhdpi_dir):
        # 如果文件已经在记录中，则跳过
        file_record = next((record for record in processed_files['xhdpi'] if record['filename'] == filename), None)
        if file_record and file_record['status'] in ('renamed', 'copied'):
            print(f'\n跳过已处理文件: {filename}')
            continue
        name, ext = os.path.splitext(filename)
        
        print(f'\n当前文件名: {filename}')
        new_name = input('请输入新文件名(无需扩展名,直接回车跳过): ').strip()
        
        # 如果输入为空则跳过当前文件
        if not new_name:
            print('跳过当前文件')
            continue
            
        # 构建完整的新文件名（带扩展名）
        new_filename = new_name + ext
        
        # 重命名 xhdpi 中的文件
        old_path_xhdpi = os.path.join(xhdpi_dir, filename)
        new_path_xhdpi = os.path.join(xhdpi_dir, new_filename)
        try:
            os.rename(old_path_xhdpi, new_path_xhdpi)
            print(f'xhdpi 文件已重命名: {filename} -> {new_filename}')
        except Exception as e:
            print(f'xhdpi 文件重命名失败: {str(e)}')
        
        # 重命名 xxhdpi 中的对应文件
        old_path_xxhdpi = os.path.join(xxhdpi_dir, filename)
        new_path_xxhdpi = os.path.join(xxhdpi_dir, new_filename)
        if os.path.exists(old_path_xxhdpi):
            try:
                os.rename(old_path_xxhdpi, new_path_xxhdpi)
                print(f'xxhdpi 文件已重命名: {filename} -> {new_filename}')
            except Exception as e:
                print(f'xxhdpi 文件重命名失败: {str(e)}')
            
            # 更新已处理文件记录
            record_file = os.path.join(current_dir, 'processed_files.json')
            processed_files = {'xhdpi': [], 'xxhdpi': []}
            if os.path.exists(record_file):
                with open(record_file, 'r', encoding='utf-8') as f:
                    processed_files = json.load(f)
            
            # 添加新文件名到记录
            new_file_record = {'filename': new_filename, 'status': 'renamed'}
            processed_files['xhdpi'].append(new_file_record)
            processed_files['xxhdpi'].append(new_file_record)
            
            # 保存更新后的记录
            with open(record_file, 'w', encoding='utf-8') as f:
                json.dump(processed_files, f, indent=2, ensure_ascii=False)
        else:
            print(f'警告: xxhdpi 中未找到对应文件 {filename}')

if __name__ == '__main__':
    print('开始重命名文件...')
    print('提示：直接按回车跳过当前文件')
    rename_resources()
    print('\n重命名完成！')